package com.voltmao.webpro.utils.wechat.utils.dto.pay;

import com.voltmao.webpro.utils.DateUtil;
import com.voltmao.webpro.utils.PropertiesUtils;
import com.voltmao.webpro.utils.StringUtils;
import com.voltmao.webpro.utils.XMLUtils;
import com.voltmao.webpro.utils.wechat.core.Account;
import com.voltmao.webpro.utils.wechat.core.Signature;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashMap;


/**
 * Created by zhouyang on 2017/2/21.
 * <p>
 * 红包请求对象
 */
public class RedPackMsg {

    public enum ScennType {
        PRODUCT_1,      //商品促销
        PRODUCT_2,      //抽奖
        PRODUCT_3      //虚拟物品兑奖
    }

    private String nonce_str;           //随机数
    private String mch_billno;          //", mch_billno);//商户订单号,组成：mch_id+yyyymmdd+10位一天内不能重复的数字。接口根据商户订单号支持重入，如出现超时可再调用。
    private String mch_id;              //商户号
    private String wxappid;             //wxAppid
    private String send_name;           //红包发送者名称
    private String scene_id;            //scene_id  PRODUCT_1:,PRODUCT_2:； PRODUCT_3:； PRODUCT_4:企业内部福利； PRODUCT_5:渠道分润；PRODUCT_6:保险；PRODUCT_7:彩票派奖；PRODUCT_8:税务刮奖

    private String sign;

    private String re_openid;           //接受红包的用户,用户在wxappid下的openid
    private String total_amount;        //付款金额，单位分
    private String total_num;           //红包发放总人数
    private String client_ip;           //调用接口的机器Ip地址
    private String act_name;            //活动名称
    private String wishing;             //红包祝福语
    private String remark;              //备注信息


    public RedPackMsg() {
        nonce_str = Signature.randomString();
        scene_id = "PRODUCT_5";
    }

    public RedPackMsg(ScennType scennType) {
        nonce_str = Signature.randomString();
        scene_id = scennType.toString();
    }

    /**
     * 加载微信账户信息
     *
     * @param account
     */
    public void initAccount(Account account) {
        this.mch_id = account.getMerId();
        this.mch_billno = this.mch_id + DateUtil.getDateStr(new Date(), "yyyyMMdd") + StringUtils.randomIntLength(10);
        this.wxappid = account.getAppId();
        this.send_name = account.getMerName();

        //加密
        this.signatureMD5(account.getPayKey());
    }

    /**
     * 转换为XML形式
     *
     * @return
     */
    public String toXml() {
        return XMLUtils.parseXml(this);
    }

    /**
     * 签名
     *
     * @param payKey
     */
    private void signatureMD5(String payKey) {

        try {
            HashMap<String, Object> map = (HashMap<String, Object>) PropertiesUtils.objectToMap(this);
            this.sign = Signature.signatureMD5(map, payKey).toUpperCase();

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public String getNonce_str() {
        return nonce_str;
    }

    public String getMch_billno() {
        return mch_billno;
    }

    public String getMch_id() {
        return mch_id;
    }

    public String getWxappid() {
        return wxappid;
    }

    public String getRe_openid() {
        return re_openid;
    }

    public String getTotal_amount() {
        return total_amount;
    }

    public String getTotal_num() {
        return total_num;
    }

    public String getClient_ip() {
        return client_ip;
    }

    public String getAct_name() {
        return act_name;
    }

    public String getSend_name() {
        return send_name;
    }

    public String getWishing() {
        return wishing;
    }

    public String getScene_id() {
        return scene_id;
    }

    public String getRemark() {
        return remark;
    }

    public String getSign() {
        return sign;
    }

    public void setRe_openid(String re_openid) {
        this.re_openid = re_openid;
    }

    public void setTotal_amount(String total_amount) {
        this.total_amount = total_amount;
    }

    public void setTotal_num(String total_num) {
        this.total_num = total_num;
    }

    public void setClient_ip(String client_ip) {
        this.client_ip = client_ip;
    }

    public void setAct_name(String act_name) {
        this.act_name = act_name;
    }

    public void setWishing(String wishing) {
        this.wishing = wishing;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }
}
